﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml;
using Ionic.Zip;
using System.IO;

namespace Dashboard
{
    public class GenerateBackup
    {
        private BackgroundWorker backgroundWorker = null;
        private XmlDocument xReader = null;
        public GenerateBackup()
        {
            backgroundWorker = new BackgroundWorker();
            backgroundWorker.WorkerReportsProgress = true;
            backgroundWorker.WorkerSupportsCancellation = true;
            backgroundWorker.DoWork += backgroundWorker_DoWork;
            backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
            backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
        }

        void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Start();
        }

        void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            
        }

        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            System.Threading.Thread.Sleep(1000);
            xReader = new XmlDocument();
            xReader.Load(Path.GetFullPath(Path.GetFullPath("Configuration.xml")));
            DateTime schDate = Convert.ToDateTime(xReader.SelectSingleNode("backup/settings/scheduledtime").InnerText);            
            if (Convert.ToInt32(schDate.Subtract(DateTime.Now).Duration().TotalSeconds) == 0)
            {
                Backup();
            }
        }

        public void Start()
        {
            backgroundWorker.RunWorkerAsync();
        }

        private void Backup()
        {
            xReader = new XmlDocument();
            xReader.Load(Path.GetFullPath("Configuration.xml"));
            var BackupFolder = xReader.SelectSingleNode("backup/settings/backupfolder").InnerText;
            var ProjectFolders = xReader.SelectNodes("backup/projectfolder/folder");
            foreach (XmlNode folder in ProjectFolders)
            {
                var path = folder.SelectSingleNode("path").InnerText;
                if (Convert.ToBoolean(folder.SelectSingleNode("active").InnerText) && Directory.Exists(path))
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(path);
                    var zipName = BackupFolder + "\\" + dirInfo.Name + DateTime.Now.ToString("_yyyy_MMM_dd_HHmmss") + ".zip";
                    ZipFile zipFile = new ZipFile(zipName);
                    zipFile.AddDirectory(path);
                    zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
                    zipFile.CompressionMethod = CompressionMethod.Deflate;
                    zipFile.UseZip64WhenSaving = Zip64Option.Always;
                    zipFile.Save(zipName);
                }
            }
        }
    }
}
